package com.example.smartcity.logic

import android.util.Log
import androidx.lifecycle.liveData
import com.example.smartcity.logic.net_work.Response
import com.example.smartcity.logic.utils.Toast.Companion.showToast
import kotlinx.coroutines.Dispatchers
import okhttp3.Request
import okhttp3.RequestBody
import kotlin.coroutines.CoroutineContext

object Repository {
    /**
     * 获取轮播图
     */
    fun getSlideshow(map: Map<String, Int>) = fire(Dispatchers.IO){
        val place = Response.getSlideshow(map)
        if (place.code == "200"){
            val rows = place.rows
            Result.success(rows)
        }else{
            Result.failure(RuntimeException("轮播图获取错误 is ${place.code}"))
        }
    }


    /**
     * 获取全部服务
     */
    fun getMore(Is: String) = fire(Dispatchers.IO){
        val place = Response.getMore(Is)
        if (place.code == "200"){
            val rows = place.rows
            Result.success(rows)
        }else{
            Result.failure(RuntimeException("获取主页全部服务失败! ${place.code}"))
        }
    }


    /**
     * 获取主页热门主题
     */
    fun getNewS(type: String) = fire(Dispatchers.IO){
        val place = Response.getNewS(type)
        if (place.code == "200"){
            val rows = place.rows
            Result.success(rows)
        }else{
            Result.failure(RuntimeException("------ 获取主页热门主题失败！"))
        }
    }

    /**
     * 获取主页热门主题
     */
    fun getNewSEntityType(type: Int) = fire(Dispatchers.IO){
        val place = Response.getNewSEntityType(type)
        if (place.code == "200"){
            val rows = place.rows
            Result.success(rows)
        }else{
            Result.failure(RuntimeException("------ 获取主页热门主题失败！"))
        }
    }


    /**
     * 获取新闻专栏
     */
    fun getNewSTitle() = fire(Dispatchers.IO){
        val place = Response.getNewSTitle()
        if(place.code == "200"){
            val rows = place.data
            Result.success(rows)
        }else {
            Result.failure(RuntimeException("------- 新闻专栏获取失败！"))
        }
    }


    /**
     * 获取全部新闻
     */
    fun getNewsAll() = fire(Dispatchers.IO){
        val place = Response.getNewsAll()
        if(place.code == "200"){
            val rows = place.rows
            Result.success(rows)
        }else Result.failure(RuntimeException("获取全部新闻失败！"))
    }


    /**
     * 获取用户全部信息
     */
    fun getLoginAll(token: String) = fire(Dispatchers.IO){
        val place = Response.getLoginAll(token)
        if(place.code == 200){
            val rows = place.user
            Result.success(rows)
        }else Result.failure(RuntimeException("获取用户信息失败！"))
    }


    /**
     * 获取反馈列表
     */
    fun getFeedBackAll(token: String) = fire(Dispatchers.IO){
        val place = Response.getFeedBackAll(token)
        if (place.code == "200"){
            val rows = place.rows
            Result.success(rows)
        }else Result.failure(RuntimeException("获取反馈列表失败！"))

    }


    /**
     * 获取新闻评论列表
     */
    fun getComment() = fire(Dispatchers.IO){
        val place = Response.getComment()
        if (place.code == 200){
            val data = place.rows
            Result.success(data)
        }else {
            "加载评论失败！".showToast()
            Result.failure(RuntimeException("获取新闻评论失败！"))
        }
    }


    // 获取新闻评论详细内容
    fun getCommentAll(id: Int) = fire(Dispatchers.IO){
        val place = Response.getCommentAll(id)
        if (place.code == 200){
            Result.success(place.data)
        }else Result.failure(RuntimeException("获取新闻评论详细内容失败！"))
    }


    /**
     * 封装
     */
    private fun <T> fire(context: CoroutineContext, block: suspend () -> Result<T>) =
        liveData<Result<T>>(context) {
            val result = try {
                block()
            }catch (e: Exception){
                Result.failure<T>(e)
            }
            emit(result)
        }

}